Ext.define('DragonApp.view.login.Controller', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.login',

    requires: [
        'Ext.util.Cookies',
        'DragonApp.util.Util',
        'DragonApp.view.main.Main',
        'DragonApp.store.JsonPServer',
        'DragonApp.ux.session.SessionMonitor'
    ],

    /**
     * Cookie字段自动赋值函数
     *
     * @return   void
     */
    setCookieField: function() {

        var existCookie = Ext.util.Cookies.get('is_setCookie');

        var field_User = Ext.ComponentQuery.query('textfield[fieldLabel="用户名"]')[0];
        var field_Pass = Ext.ComponentQuery.query('textfield[fieldLabel="密码"]')[0];
        var field_remember_me = Ext.ComponentQuery.query("checkbox#remember_me")[0];

        //如果系统已设置了Cookie则字段自动赋值
        if ('true' === existCookie) {

            field_User.setValue(Ext.util.Cookies.get('user'));
            field_Pass.setValue(Ext.util.Cookies.get('pass'));
            field_remember_me.setValue('true');
        }
    },

    /**
     * 登录
     *
     * @param    {object} loginWindow 登录窗体
     * @return   void
     */
    doLogin: function(loginWindow) {

        var me = this;

        var form = Ext.ComponentQuery.query('loginform')[0].getForm();
        var formValue = form.getValues();

        //判断账户密码存储选择，设置Cookie
        var field_remember_me = Ext.ComponentQuery.query("checkbox#remember_me")[0];
        var field_remember_me_value = field_remember_me.getSubmitValue();


        // 是否记忆用户名
        if ("true" == field_remember_me_value) {

            var datas,user, pass;
            datas = user = pass = [];

            fields =  [Ext.ComponentQuery.query("textfield", loginWindow)[0]];
            DragonApp.util.Util.setCookie(fields, 7);

        } else if ("false" == field_remember_me_value) {

            DragonApp.util.Util.clearCookie(['user', 'pass', 'is_setCookie']);
        }

        // 模态提示
        loginWindow.mask('登录中...');

        Ext.data.JsonP.request({

            url: DragonApp.ux.config.Config.getUrl('/index/Account/doLogin/'),
            async: false,
            params: formValue,
            callbackKey: "callback",
            success: function(result) {

                switch (result.success) {
                    case true: //登录成功

                        sessionStorage.setItem("DragonApp", true);
                        sessionStorage.setItem("DragonApp_user", result.data.user);
                        sessionStorage.setItem("DragonApp_role_name", result.data.role_name);

                        loginWindow.unmask();
                        loginWindow.destroy();

                        //主界面
                        var main = Ext.create('DragonApp.view.main.Main', {
                            renderTo: Ext.getBody()
                        });


                        //工具栏
                        var tbar = main.items.first();

                        //工具栏赋值
                        var tbar_user = tbar.queryById('user');
                        var tbar_role_name = tbar.queryById('role_name');

                        tbar_user.setText(result.data.user);
                        tbar_role_name.setText(result.data.role_name);

                        //计时监控
                        DragonApp.ux.session.SessionMonitor.start();

                        break;

                    case false: //登录失败
                        DragonApp.util.Msg.showToast(result.msg);
                        loginWindow.unmask();
                        break;

                    default:
                        loginWindow.unmask();
                        DragonApp.util.Msg.showToast(result.msg);
                        break;
                }

            },
            failure: function(result) {
                loginWindow.unmask();
                DragonApp.util.Msg.showToast(result);
            }
        });
    },
    /**
     * 提交
     *
     * @param    {[type]} button [description]
     * @param    {[type]} e [description]
     * @param    {[type]} options [description]
     * @return   void
     */
    onSubmitClick: function(button, e, options) {

        var me = this;
        var loginWindow = this.getView();

        if (Ext.ComponentQuery.query('loginform')[0].isValid()) {
            me.doLogin(loginWindow);
        }
    },
    /**
     * 取消
     *
     * @param    {[type]} button [description]
     * @param    {[type]} e [description]
     * @param    {[type]} options [description]
     * @return   void
     */
    onCancelClick: function(button, e, options) {

        Ext.ComponentQuery.query('loginform')[0].reset();
    },

    /**
     * 文本框登录
     *
     * @param    {[type]} field [description]
     * @param    {[type]} e [description]
     * @param    {[type]} options [description]
     * @return   void
     */
    onTextFieldSpecialKey: function(field, e, options) {

        var me = this;
        var loginWindow = this.getView();

        if (e.getKey() === e.ENTER) {
            if (Ext.ComponentQuery.query('loginform')[0].isValid()) {
                this.doLogin(loginWindow);
            }
        }
    },

    /**
     * 密码框登录
     *
     * @param    {object} field [description]
     * @param    {[type]} e [description]
     * @param    {[type]} options [description]
     * @return   void
     */
    onTextFieldKeyPress: function(field, e, options) {

        var charCode = e.getCharCode(),
            me = this;

        //密码框 Caps Lock 键激活判断
        if ((e.shiftKey && charCode >= 97 && charCode <= 122) ||
            (!e.shiftKey && charCode >= 65 && charCode <= 90)) {

            Ext.toast({
                title: '<div class="fa fa-exclamation-triangle">Caps Lock 键正被激活,输入密码前,请单击关闭。</div>',
                width: 400,
                align: 't'
            });
        }

        var loginWindow = this.getView();

        if (e.getKey() === e.ENTER) {
            if (Ext.ComponentQuery.query('loginform')[0].isValid()) {
                this.doLogin(loginWindow);
            }
        }
    }
});